<?php
namespace app\controller;

use think\exception\HttpResponseException;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\View;

class Base{
    public function __construct()
    {
        $admin_id = Cookie::get('admin_id');
        if(empty($admin_id)){
            header('location:/login/index');
            exit();
        }
        $admin = Db::table('admin_users')->where('id',$admin_id)->find();
        if(empty($admin) || $admin['status']!=1){
            Cookie::delete('admin_id');
            header('location:/login/index');
            exit();
        }

//        if(empty($admin)){
//            Cookie::delete('admin_id');
//            header('location:/login/index');
//            exit();
//        }
//        if($admin['status']!=1){
//            Cookie::delete('admin_id');
//            $this->error("账号异常,请联系管理员");
//        }
        View::assign(['admin'=>$admin]);
    }

    public function error($msg){
        $arr = [
            'code' => 0,
            'msg' => $msg
        ];
        $response = view(Config::get('app.exception_error_tmpl'),$arr);
        throw new HttpResponseException($response);
    }

}
